package com.yd.controller.app;
//提交测试

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Dongtais;
import com.yd.controller.yd.YD_Jifen;
import com.yd.pojo.Users;
import com.yd.service.UsersService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;
import com.yd.tool.UtsUuid;


@Controller
public class UsersControllerApp {
 
	@Resource
	private UsersService usersService;

	//用户注册
	@RequestMapping(value = { "/app/reg" })
	@ResponseBody
	public String app_reg(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		//接受参数
		  //用户名称
		  //密码
		  //再次密码
		  //邀请码 可为空
		//验证参数
		//验证两次密码是否一致
		//验证用户名是否已经存在
		//注册-添加用户信息
		//调用注册积分接口 传自己id 送给他自己积分
		//判断上级是否存在
		  //存在
		      //更新用户的上级
		      //获取上级用户信息
		      //调用邀请积分接口 传上级id

		String loginname = "";
		String loginpass = "";
		String loginpass2 = "";
		String invitecode = "";//邀请码-手机号
		if (UtsReg.getIsPhone(request.getParameter("loginname"))) {loginname = request.getParameter("loginname").trim();} else {jsonObject.put("msg", "手机号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringPass(request.getParameter("loginpass"))) {loginpass = request.getParameter("loginpass").trim();} else {jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringPass(request.getParameter("loginpass2"))) {loginpass2 = request.getParameter("loginpass2").trim();} else {jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIsPhone(request.getParameter("invitecode"))) {invitecode = request.getParameter("invitecode").trim();} else {jsonObject.put("msg", "邀请码格式错误");jsonObject.put("error", "1");}
		
		if(!loginpass.equals(loginpass2)){
			jsonObject.put("msg", "两次密码不一致");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(usersService.selectCount(new EntityWrapper<Users>().eq("loginname", loginname))>0){
			jsonObject.put("msg", "账号已经存在1111");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Users users = new Users();
		users.setPhone(loginname);
		users.setLoginname(loginname);
		users.setLoginpass(MD5Hash.getMd5HashPasswod(loginpass));
		if (!usersService.insert(users)) {
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			
		}
		YD_Jifen.getzhucheUsersByUserid(users.getId()+"");
		EntityWrapper<Users> ew = new EntityWrapper<Users>();
		if (ew.eq("invitecode", loginname) == null) {
			jsonObject.put("msg", "该商户不存在上级");jsonObject.put("error", "1");
		}else {
			users.setInvitecode(invitecode);
			Users user=usersService.selectOne(new EntityWrapper<Users>().eq("loginname", invitecode));
			users.setTpid(user.getId()+"");
			YD_Jifen.getappyaoqingUsersByUserid(user.getId()+"");
		}
		
		if (!usersService.updateById(users)) {
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}

	//用户登录
	@RequestMapping(value = "/app/login")
	@ResponseBody
	public String app_login(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		//获取参数
		String loginname="";
		String loginpass="";
		//验证参数
		if (UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "账号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass = request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		//通过账号查询此条数据
		Users users=usersService.selectOne(new EntityWrapper<Users>().eq("loginname", loginname));
		//判断此账号是否有数据
		if(users==null){//如果没有数据 那说明不存在此账号
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//走到这里说明有账号，那么要判断这个账号的密码是否正确，传过来的密码要加密后再对比
		if(!users.getLoginpass().equals(MD5Hash.getMd5HashPasswod(loginpass))){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//生成一个随机数当作token
		users.setToken(UtsUuid.getUuid());
		//把这个token更新到数据库，每次登陆都要重新生成一次
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//把实体类里面的密码清空 然后返回给前台(所有返回用户信息的时候都要清空重要的信息，比如密码，身份证，等等)
		users.setLoginpass("");
		jsonObject.put("data", users);
		
		//YD_Jifen.getUsersByUserid(users.getId()+"");
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//获取用户token信息
	@RequestMapping(value = "/app/user/token")
	@ResponseBody
	public String app_user_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Users users=usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		users.setLoginpass("");
		jsonObject.put("data", users);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//用户密码修改
	@RequestMapping("/app/user/pass/up")
	@ResponseBody
	public String app_user_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String ypass = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("ypass"))){ypass=request.getParameter("ypass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Users users = usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(ypass).equals(users.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		users.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!usersService.updateById(users)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	//用户头像修改
	@RequestMapping("/app/user/touxiang/up")
	@ResponseBody
	public String app_user_touxiang_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String img = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setImg(img);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		//修改发布动态表里面的头像
	    //调用远程动态表修改头像方法 把用户id和用户img名称传过去
	    //无需判断是否修改成功
		//验证ID
		YD_Dongtais.getUsersByUserid(users.getId()+"");
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户性别修改
	@RequestMapping("/app/user/sex/up")
	@ResponseBody
	public String app_user_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String sex = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		if ("男".equals(sex)) {
			users.setSex(sex);
		}else if("女".equals(sex)){
			users.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!usersService.updateById(users)){
			jsonObject.put("sex", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户年龄修改
	@RequestMapping("/app/user/age/up")
	@ResponseBody
	public String app_user_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String age = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setAge(age);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户昵称修改
	@RequestMapping("/app/user/nicheng/up")
	@ResponseBody
	public String app_user_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String nicheng = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setNicheng(nicheng);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户名字修改
	@RequestMapping("/app/user/name/up")
	@ResponseBody
	public String app_user_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String name = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setName(name);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户邮箱修改
	@RequestMapping("/app/user/email/up")
	@ResponseBody
	public String app_user_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String email = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setEmail(email);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//用户地址修改
	@RequestMapping("/app/user/address/up")
	@ResponseBody
	public String app_user_address_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String address = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("address"))){address = request.getParameter("address").trim();}else{jsonObject.put("msg", "地址格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("email", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setAddress(address);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//查询热门商家
	@RequestMapping(value = {"/app/users/list"})
	@ResponseBody
	public String app_users_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String tid="";
		String page="";
		String size="";
		
		if(UtsReg.getInt(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}
		if(UtsReg.getStringKB(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getStringKB(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		
		List<Users> list = null;
		EntityWrapper<Users> ew=new EntityWrapper<Users>();
		if(tid.length()>0){
			ew.eq("tid", tid);
		}
		list=usersService.selectList(ew.eq("tid", tid).orderBy("uptime", false));
		PageInfo<Users> pages = new PageInfo<Users>(list);
		
		//用户返回的时候必须屏蔽掉用户隐私信息
		for (int i = 0; i < pages.getList().size(); i++) {
			Users u=pages.getList().get(i);
			u.setLoginpass("");//密码
			u.setToken("");//token
		} 

		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//商户提交个人信息
	@RequestMapping("/app/users/personal/up")
	@ResponseBody
	public String app_users_personal_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String tid = "";
		String gongsiname = "";
		String gongsilogo = "";
		String gongsitel = "";
		String address = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getInt(request.getParameter("tid"))){tid = request.getParameter("tid").trim();}else{jsonObject.put("msg", "类别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("gongsiname"))){gongsiname = request.getParameter("gongsiname").trim();}else{jsonObject.put("msg", "公司名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("gongsilogo"))){gongsilogo = request.getParameter("gongsilogo").trim();}else{jsonObject.put("msg", "公司图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("gongsitel"))){gongsitel = request.getParameter("gongsitel").trim();}else{jsonObject.put("msg", "公司电话格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("address"))){address = request.getParameter("address").trim();}else{jsonObject.put("msg", "公司地址格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		if (usersService.selectCount(new EntityWrapper<Users>().eq("gongsiname", gongsiname))>0) {jsonObject.put("msg", "该公司名已存在");jsonObject.put("error", "1");return jsonObject.toString();}
		if (usersService.selectCount(new EntityWrapper<Users>().eq("gongsitel", gongsitel))>0) {jsonObject.put("msg", "该公司电话已存在");jsonObject.put("error", "1");return jsonObject.toString();}
		users.setTid(tid);
		users.setGongsilogo(gongsilogo);
		users.setGongsitel(gongsitel);
		users.setAddress(address);
		users.setGongsiname(gongsiname);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//商户查询自己的下级
	@RequestMapping("/app/users/tpid/list")
	@ResponseBody
	public String app_users_tpid_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("token", token));
		if(users==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		List<Users> list = usersService.selectList(new EntityWrapper<Users>().eq("tpid", users.getId()));
		if(list.size()!=0){
			List<String> aa=new ArrayList<String>();//aa=新输出的名字的数组
			for(int i=0;i<list.size();i++){//xiajimz=下级名字
				String xiajimz=list.get(i).getLoginname();
				aa.add(xiajimz);
			}
			jsonObject.put("msg", "成功");
			jsonObject.put("data",aa);
			return jsonObject.toString();
		}else {
			jsonObject.put("msg", "该商户不存在下级");
			jsonObject.put("error", "1");
		}
		return jsonObject.toString();
		
	}
	
}
